package com.titleevaluation.service;

import com.titleevaluation.entity.Application;
import com.titleevaluation.entity.IndustryQualifications;
import com.titleevaluation.repository.ApplicationRepository;
import com.titleevaluation.repository.IndustryQualificationsRepository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class IndustryQualificationsService {

    private final IndustryQualificationsRepository industryQualificationsRepository;
    private final ApplicationRepository applicationRepository;

    public IndustryQualificationsService(IndustryQualificationsRepository industryQualificationsRepository, ApplicationRepository applicationRepository) {
        this.industryQualificationsRepository = industryQualificationsRepository;
        this.applicationRepository = applicationRepository;
    }

    public IndustryQualifications createIndustryQualifications(Long applicationId, IndustryQualifications industryQualifications) {
        Application application = applicationRepository.findById(applicationId)
                .orElseThrow(() -> new RuntimeException("Application not found"));
        industryQualifications.setApplication(application);
        industryQualifications.setCreatedAt(LocalDateTime.now());
        industryQualifications.setUpdatedAt(LocalDateTime.now());
        return industryQualificationsRepository.save(industryQualifications);
    }

    public List<IndustryQualifications> getIndustryQualificationsByApplicationId(Long applicationId) {
        return industryQualificationsRepository.findByApplicationId(applicationId);
    }

    public Optional<IndustryQualifications> getIndustryQualificationsById(Long id) {
        return industryQualificationsRepository.findById(id);
    }

    public IndustryQualifications updateIndustryQualifications(Long id, IndustryQualifications updatedIndustryQualifications) {
        IndustryQualifications existingIndustryQualifications = industryQualificationsRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("IndustryQualifications not found"));

        existingIndustryQualifications.setCertificateType(updatedIndustryQualifications.getCertificateType());
        existingIndustryQualifications.setCertificateName(updatedIndustryQualifications.getCertificateName());
        existingIndustryQualifications.setIssuingAuthority(updatedIndustryQualifications.getIssuingAuthority());
        existingIndustryQualifications.setCertificateNo(updatedIndustryQualifications.getCertificateNo());
        existingIndustryQualifications.setIssuingUnit(updatedIndustryQualifications.getIssuingUnit());
        existingIndustryQualifications.setJobTitle(updatedIndustryQualifications.getJobTitle());
        existingIndustryQualifications.setRemarks(updatedIndustryQualifications.getRemarks());
        existingIndustryQualifications.setUpdatedAt(LocalDateTime.now());

        return industryQualificationsRepository.save(existingIndustryQualifications);
    }

    public void deleteIndustryQualifications(Long id) {
        industryQualificationsRepository.deleteById(id);
    }
}

